<template>
  <div class="fui-content" style="height: 854px; background: #f3f3f3">
    <div class="content-empty" v-if="shop_show == 0">
      <img
        class="img"
        src="http://d.vsd.cc/addons/ewei_shopv2/static/images/nogoods.png"
      />
      <div class="title">您的购物车中没有商品哦！</div>
      <button style="width: 50%; margin-top: 1rem">去看看商品吧</button>
    </div>
    <div v-if="shop_show == 1" style="padding-bottom: 64px">
      <div
        class="fui-list goods-item align-start"
        v-for="item in arr"
        :key="item.id"
      >
        <div class="fui-list-media">
          <input
            name="sid"
            type="checkbox"
            v-model="item.check"
            @change="
              addSub({ sid: item.sid, type: 'noChange', check: item.check })
            "
          />
        </div>
        <div class="fui-list-media image-media">
          <img class="img" :src="item.good?.img.split(';')[0]" />
        </div>
        <div class="fui-list-inner">
          <div class="subtitle">
            {{ item.good?.title }}
          </div>
          <div class="price" style="margin-top: 30px">
            <span class="bigprice text-danger">
              <span>¥</span>
              <span>{{ item.good?.price }}</span>
            </span>
            <div class="fui-number small">
              <button
                class="minus"
                @click="
                  addSub({ sid: item.sid, type: 'sub', check: item.check })
                "
              >
                -
              </button>
              <input class="num" disabled="true" :value="item.num" />
              <button
                class="plus"
                @click="
                  addSub({ sid: item.sid, type: 'add', check: item.check })
                "
              >
                +
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="fui-footer">
    <div class="fui-footer-list">
      <div
        class="editmode"
        style="
          font-size: 0.7rem;
          color: #666;
          display: flex;
          align-items: center;
        "
      >
        <input
          name="Fruit"
          type="checkbox"
          @change="checkAll"
          style="margin-right: 5px"
        />全选
      </div>
      <div class="fui-footer-inner">
        <div class="footer-subtitle">
          合计:<span class="text-danger size0-75rem"> ￥{{ totalMoney }}</span>
        </div>
      </div>
      <div class="fui-list-angle">
        <button class="btn" @click="order()">结算(0)</button>
      </div>
    </div>
  </div>
</template>
<script>
import { inject, reactive, toRefs, computed } from "vue";
import { cartIndex, AddCart } from "../../network/index.js";

export default {
  setup() {
    const router = inject("router");

    const data = reactive({
      arr: [],
    });

    const totalMoney = computed(() => {
      return data.arr
        .filter((item) => item.check)
        .reduce((total, item) => {
          return total + item.good.price * item.num;
        }, 0);
    });

    const getCartIndex = () => {
      cartIndex().then((res) => {
        data.arr = res;
      });
    };

    getCartIndex();

    const addSub = (data) => {
      AddCart(data).then((res) => {
        getCartIndex();
      });
    };

    const checkAll = (e) => {
      if (e.target.checked) {
        data.arr.forEach((item) => {
          item.check = true;
        });
      } else {
        data.arr.forEach((item) => {
          item.check = false;
        });
      }
    };

    const order = () => {
      const newArr = data.arr.filter((item) => item.check);
      const shop_id = newArr.map((item) => item.sid).join(";");
      const num = newArr.map((item) => item.num).join(";");
      router.push(`/cart_order?id=${shop_id}&num=${num}&type=2`);
    };

    return {
      order,
      shop_show: 1,
      ...toRefs(data),
      addSub,
      checkAll,
      totalMoney,
    };
  },
};
</script>

<style scoped>
.fui-content {
  position: absolute;
  right: 0;
  left: 0;
  overflow: auto;
  padding-bottom: 2.2rem;
}
.content-empty {
  position: relative;
  text-align: center;
  margin-top: 3rem;
  color: #ccc;
  -webkit-box-align: center;
}
.img {
  width: 198rpx;
  margin-bottom: 0.5rem;
}
.title {
  color: #999;
  font-size: 0.75rem;
}
.url {
  border-radius: 100px;
  height: 1.9rem;
  line-height: 1.9rem;
  width: 7rem;
  font-size: 0.75rem;
}
/* 有商品 */
.fui-list {
  position: relative;
  display: flex;
  transition-duration: 300ms;
  align-items: center;
  overflow: hidden;
  -webkit-box-align: start;
  padding: 0.65rem 0.6rem;
  margin-top: 0.5rem;
  background-color: #fff;
}
.fui-list-media {
  display: flex;
  flex-shrink: 0;
  -webkit-box-lines: single;
  flex-wrap: nowrap;
  box-sizing: border-box;
  -webkit-box-align: center;
  align-items: center;
  margin-right: 0.6rem;
  color: #aaa;
  position: relative;
}
.img {
  width: 86px;
  height: 86px;
}
image-media {
  margin-left: 1rem;
  align-self: center;
}
.fui-list-inner {
  position: relative;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  flex: 1;
  justify-content: space-between;
  flex-direction: column;
  -webkit-box-pack: justify;
  min-height: 4.6rem;
  align-self: center;
}
.subtitle {
  align-items: center;
  position: relative;
  font-size: 0.7rem;
  margin-top: 0.2rem;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
  color: #000;
}
.price {
  display: flex;
  -webkit-box-pack: justify;
  justify-content: space-between;
  height: 1.2rem;
  font-size: 0.65rem;
  color: #000;
}
.bigprice {
  font-size: 0.75rem;
}
.text-danger {
  color: #ff5555;
}
.fui-number {
  backface-visibility: hidden;
  box-sizing: border-box;
  position: relative;
  display: flex;
  font-size: 0.8rem;
  -webkit-box-pack: justify;
  justify-content: space-between;
  margin: 0;
}
.small {
  height: 1.1rem;
  width: 79px;
  line-height: 1.2rem;
}
.minus {
  height: inherit;
  font-weight: bold;
  position: relative;
  text-align: center;
  z-index: 1;
  font-size: 0.65rem;
  color: #666;
  background: #fff;
  width: 1.1rem;
  line-height: 1.1rem;
}
.num {
  -webkit-box-flex: 1;
  flex: 1;
  overflow: hidden;
  line-height: inherit;
  color: #666;
  text-align: center;
  border: 0;
  font-size: 0.7rem;
}
.plus {
  height: inherit;
  font-weight: bold;
  position: relative;
  text-align: center;
  z-index: 1;
  font-size: 0.65rem;
  color: #666;
  background: #fff;
  width: 1.1rem;
  line-height: 1.1rem;
}
/* 底部按钮 */
.fui-footer {
  position: fixed;
  width: 100%;
  height: 2.45rem;
  background: #fff;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
  -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
  -moz-box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
  z-index: 2;
  padding: 0 0 90px 0;
  margin: 0;
  bottom: 0;
}
.fui-footer-list {
  align-items: center;
  overflow: hidden;
  transition-duration: 300ms;
  -webkit-box-align: center;
  display: flex;
  position: relative;
  justify-content: space-between;
  padding: 0 0 0 0.6rem;
  height: 2.45rem;
  -webkit-box-pack: justify;
}
.editmode {
  align-items: center;
  margin-right: 0.6rem;
  color: #aaa;
  position: relative;
  flex-wrap: nowrap;
  box-sizing: border-box;
  -webkit-box-align: center;
  flex-shrink: 0;
  -webkit-box-lines: single;
}
.fui-footer-inner {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  margin: 0;
  padding: 0;
  position: relative;
  font-size: 0.7rem;
  color: #666;
  padding-left: 1.2rem;
  vertical-align: middle;
}
.footer-subtitle {
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
  color: #000;
  font-size: 0.7rem;
  margin-top: 0.2rem;
  display: flex;
  align-items: flex-end;
  padding: 0;
  justify-content: flex-end;
  -webkit-box-pack: end;
}
.fui-list-angle {
  position: relative;
  vertical-align: middle;
  padding: 0;
  display: flex;
  margin: 0;
}
.btn {
  background: #ff5555;
  border: 0;
  padding: 0;
  margin: 0;
  border-radius: 0;
  height: 100%;
  line-height: 2.45rem;
  font-size: 0.7rem;
  color: #fff;
  margin-left: 0.05rem;
  width: 5rem;
}
</style>